﻿using LinphoneEngine;
using LinphoneEngine.Listeners;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinPhoneWPF
{
    public class Phone
    {
        public CoreListener Listener { get; set; }

        public Phone()
        {
            InitPhone();
        }

        public void InitPhone()
        {
            Core.SetLogLevel(OrtpLogLevel.DEBUG);
            Listener = Factory.Instance.CreateCoreListener();
            Listener.OnRegistrationStateChanged = new CoreListener.OnRegistrationStateChangedDelegate(RegisterStateChanged);
            Listener.OnCallStateChanged = new CoreListener.OnCallStateChangedDelegate(CallStateChanged);
        }

        private void RegisterStateChanged(Core lc, ProxyConfig cfg, RegistrationState cstate, string message)
        {
            
        }

        private void CallStateChanged(Core lc, Call call, CallState cstate, string message)
        {
            switch (cstate)
            {
                case CallState.OutgoingRinging:
                    Console.WriteLine("It is now ringing remotely !");
                    break;
                case CallState.OutgoingEarlyMedia:
                    Console.WriteLine("Receiving some early media");
                    break;
                case CallState.Connected:
                    Console.WriteLine("We are connected !");
                    break;
                case CallState.StreamsRunning:
                    Console.WriteLine("Media streams established !");
                    break;
                case CallState.End:
                    Console.WriteLine("Call is End.");
                    break;
                case CallState.Error:
                    Console.WriteLine("Call failure !");
                    break;
                case CallState.IncomingReceived:
                    Console.WriteLine("Incomming call received!");
                    Console.WriteLine($"Microphone volume gain: {call.MicrophoneVolumeGain}");
                    lc.AcceptCall(call);
                    break;
                case CallState.IncomingEarlyMedia:
                    Console.WriteLine("Incomming early media!");
                    break;
                case CallState.Released:
                    Console.WriteLine("Released call!");
                    break;
                default:
                    Console.WriteLine($"Unhandled notification {cstate}\n");
                    break;
            }
        }
    }
}
